//
//  VinHTTPHandle.m
//  SWCommonFrame
//
//  Created by shaowei on 13-6-18.
//  Copyright (c) 2013年 shaowei. All rights reserved.
//

#import "SWHTTPHandle.h"

@implementation SWHTTPHandle


#pragma mark Function - Private

/**
 获取系统首选语言
 @param
 @returns
 */
+ (NSString *)getSystemLanguage{
    return [[NSLocale preferredLanguages] objectAtIndex:0];
}

#pragma mark - Function - Public

/**
 判断String是否包含 “?”
 @param 原始String
 @returns 包含“?”,则yes，否则no
 */
+ (BOOL)isHaveQuestionMark:(NSString *)urlString{
    NSError *error = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[?]"
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    int resultNum = [regex numberOfMatchesInString:urlString
                                           options:0
                                             range:NSMakeRange(0, [urlString length])];
    return resultNum?YES:NO;
}

/**
 //为url添加"&"或者"?"，如果url中已有"?",则添加"&",否则添加"?"
 @param 原始String
 @returns 处理后的url
 */
+ (NSString *)handleUrlAddMark:(NSString *)urlString{
    NSString *markString = [SWHTTPHandle isHaveQuestionMark:urlString]?@"&":@"?";
    return [urlString stringByAppendingString:markString];
}

/**
 统一对所有的url加入一些额外的标识信息。
 @param 原始的urlString
 @returns 处理完后的String
 */
+ (NSString *)handleUrlAddExtraInfo:(NSString *)originUrl{
    
    //判断是否加了“?”
    NSString *markString = [SWHTTPHandle isHaveQuestionMark:originUrl]?@"&":@"?";
    
    NSString *newUrlString = [NSString stringWithFormat:@"%@%@client=%@&language=%@&doctype=%@",
                              originUrl,markString,RCLIENT,[SWHTTPHandle getSystemLanguage],RDOCTYPE];
    
    return newUrlString;
}


/**
 处理url，用于查找标识的缓存
 @param 原始urlString
 @returns 处理完的url
 */
+ (NSString *)handleUrlToMarkCache:(NSString *)originUrl{
    
    return originUrl;
//    NSString *currentLanguageCode = [VinHTTPHandle getSystemLanguage];
//    NSString *markString = [VinHTTPHandle isHaveQuestionMark:originUrl]?@"&":@"?";
//    return [NSString stringWithFormat:@"%@%@language=%@",originUrl,markString,currentLanguageCode];
}

/**
 为url添加md5参数
 @param originUrl:原始url，md5String:md5字符串
 @returns 添加完成后的url
 */
+ (NSString *)handleUrlAddMD5:(NSString *)originUrl andMD5:(NSString *)md5String{
    NSString *markString = [SWHTTPHandle isHaveQuestionMark:originUrl]?@"&":@"?";
    return [NSString stringWithFormat:@"%@%@mv=%@",originUrl,markString,md5String];
}



@end
